package com.hmall.api.config;

import cn.hutool.core.util.ObjectUtil;
import com.hmall.api.clients.fallback.ItemClientFallback;
import com.hmall.api.clients.fallback.PayClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.BASIC;
    }


    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return requestTemplate -> {
            Long userId = UserContext.getUser();
            if (ObjectUtil.isNotEmpty(userId)){
                //不为空则将UserId存入feign的请求头
                requestTemplate.header("user-info",userId.toString());
            }
        };
    }

    @Bean
    public ItemClientFallback itemClientFallback(){
        return new ItemClientFallback();
    }

    @Bean
    public PayClientFallback payClientFallback(){
        return new PayClientFallback();
    }
}
